package org.example.memora.controller;

import com.alibaba.druid.pool.ha.selector.StickyDataSourceHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.memora.DTO.UserRedisDTO;
import org.example.memora.common.result.PageResult;
import org.example.memora.common.result.Result;
import org.example.memora.common.utils.UserHolder;
import org.example.memora.service.PostCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/collects")
@Tag(name = "帖子收藏管理", description = "提供对帖子收藏与取消收藏的操作接口")
public class PostCollectController {

    @Autowired
    private PostCollectService postCollectService;

    /**
     * 收藏帖子
     */
    @Operation(
            summary = "收藏帖子",
            description = "对指定帖子进行收藏操作，如已收藏则返回错误信息。"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "收藏成功",
                    content = @Content(mediaType = "application/json")),
            @ApiResponse(responseCode = "401", description = "用户未登录或权限不足"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping
    public Result<?> collectPost(
            @Parameter(name = "postId", description = "帖子 ID", in = ParameterIn.QUERY, required = true)
            @RequestParam String postId) {
        UserRedisDTO userRedisDTO =UserHolder.getUser();

        if(userRedisDTO==null){
            return  Result.error("获取不到UserID,请检查controller");
        }
        Long userId= userRedisDTO.getUserId();
        log.info("用户 [{}] 收藏帖子 [{}]", userId, postId);
        return postCollectService.collectPost(userId, postId);
    }

    /**
     * 取消收藏
     */
    @Operation(
            summary = "取消收藏",
            description = "取消对指定帖子的收藏操作。"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "取消收藏成功",
                    content = @Content(mediaType = "application/json")),
            @ApiResponse(responseCode = "401", description = "用户未登录或权限不足"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @DeleteMapping
    public Result<?> uncollectPost(
            @Parameter(name = "postId", description = "帖子 ID", in = ParameterIn.QUERY, required = true)
            @RequestParam String postId) {
        UserRedisDTO userRedisDTO =UserHolder.getUser();

        if(userRedisDTO==null){
            return  Result.error("获取不到UserID,请检查controller");
        }
        Long userId= userRedisDTO.getUserId();
        log.info("用户 [{}] 取消收藏帖子 [{}]", userId, postId);
        return postCollectService.uncollectPost(userId, postId);
    }

    /**
     * 查询是否已收藏
     */
    @Operation(
            summary = "查询是否已收藏",
            description = "查询指定用户是否已对某帖子收藏。"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(mediaType = "application/json")),
            @ApiResponse(responseCode = "401", description = "用户未登录或权限不足"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping
    public Result<?> hasCollected(
            @Parameter(name = "postId", description = "帖子 ID", in = ParameterIn.QUERY, required = true)
            @RequestParam String postId) {
        UserRedisDTO userRedisDTO =UserHolder.getUser();

        if(userRedisDTO==null){
            return  Result.error("获取不到UserID,请检查controller");
        }
        Long userId= userRedisDTO.getUserId();
        log.info("用户 [{}] 查询帖子 [{}] 是否已收藏", userId, postId);
        boolean collected = postCollectService.hasCollected(userId, postId);
        if (collected) {
            return Result.success(1, "已收藏");
        }
        return Result.success(1, "未收藏");
    }
    @Operation(
            summary = "分页获取用户收藏的帖子",
            description = "返回当前登录用户收藏的帖子列表，支持分页参数"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功，返回收藏帖子分页结果",
                    content = @Content(mediaType = "application/json")),
            @ApiResponse(responseCode = "401", description = "用户未登录或权限不足"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/list")
    public Result<PageResult> getCollectedPosts(
            @Parameter(name = "page", description = "页码，从1开始", example = "1")
            @RequestParam(defaultValue = "1") Integer page,

            @Parameter(name = "size", description = "每页数量", example = "10")
            @RequestParam(defaultValue = "10") Integer size) {

        UserRedisDTO userRedisDTO = UserHolder.getUser();
        if (userRedisDTO == null) {
            return Result.error("获取不到UserID，请检查登录状态");
        }
        Long userId = userRedisDTO.getUserId();
        log.info("用户 [{}] 请求第 [{}] 页收藏帖子，每页 [{}] 条", userId, page, size);
        PageResult result = postCollectService.getCollectPosts(userId, page, size);
        return Result.success(result);
    }

}
